package com.hj.controller;

import android.util.Log;

import java.util.ArrayList;

/**
 * Created by hp on 2018/4/26.
 */

public abstract class Observable<T> {
    protected final ArrayList<T> mObservers = new ArrayList();

    public Observable() {
    }

    public void registerObserver(T observer) {
        if(observer == null) {
            throw new IllegalArgumentException("The observer is null.");
        } else {
            ArrayList var2 = this.mObservers;
            synchronized(this.mObservers) {
                if(this.mObservers.contains(observer)) {
                    Log.e("Observable", "Observer " + observer + " is already registered.");
                    throw new IllegalStateException("Observer " + observer + " is already registered.");
                } else {
                    this.mObservers.add(observer);
                }
            }
        }
    }

    public void unregisterObserver(T observer) {
        if(observer == null) {
            Log.e("Observable", "The observer is null.");
        } else {
            ArrayList var2 = this.mObservers;
            synchronized(this.mObservers) {
                int index = this.mObservers.indexOf(observer);
                if(index == -1) {
                    Log.e("Observable", "Observer " + observer + " was not registered.");
                } else {
                    this.mObservers.remove(index);
                }
            }
        }
    }

    public void unregisterAll() {
        ArrayList var1 = this.mObservers;
        synchronized(this.mObservers) {
            this.mObservers.clear();
        }
    }
}
